package org.iqiyi.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.iqiyi.util.ChangeVideo;
import org.iqiyi.util.CheckExcelFileTypeUtil;
import org.iqiyi.util.JsonResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.UUID;

@Api(value = "文件上传", tags = "文件上传")
@RestController
@RequestMapping("/fileUpload")
public class FileUploadController {
    @Value("${file.path}")
    private String filePath;

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam(value = "myFile") MultipartFile multipartFile) throws IOException {
        String fileType = CheckExcelFileTypeUtil.getFileType(multipartFile.getInputStream());

        if (StrUtil.isEmpty(fileType)) {
            return JsonResult.fail("不支持的文件");
        }
        if (!Files.exists(Paths.get(filePath))) {
            Files.createDirectory(Paths.get(filePath));
        }
        String file = "";
        if (fileType.equals("asf") || fileType.equals("avi") || fileType.equals("wav") || fileType.equals("rm") || fileType.equals("mpg") || fileType.equals("mov")) {
            file = UUID.randomUUID().toString() + "." + fileType;
       } else {
            file = UUID.randomUUID().toString() + "." + fileType;
        }

        FileInputStream fileInputStream = (FileInputStream) multipartFile.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/" + file);
        //获取输入流缓冲区
        FileChannel inputChannel = fileInputStream.getChannel();
        //获取输出流缓冲区
        FileChannel outputChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (true) {
            // 读取缓冲区中的内容
            int i = inputChannel.read(byteBuffer);
            // 拿到内容后转成可读模式
            byteBuffer.flip();
            // 将缓冲区的内容写到通道中进行写入
            outputChannel.write(byteBuffer);
            byteBuffer.clear();
            if (i == -1) {
                break;
            }
        }
        inputChannel.close();
        outputChannel.close();
        String copyFile = UUID.randomUUID().toString() + ".mp4";
        // 把所有视频文件转码成mp4格式
        if (fileType.equals("asf") || fileType.equals("wav") || fileType.equals("avi") || fileType.equals("wav") || fileType.equals("rm") || fileType.equals("mpg") || fileType.equals("mov")) {
            ChangeVideo.convert(filePath + "/" + file, filePath + "/" + copyFile);
            file = copyFile;
        }
        return JsonResult.success(file);
    }

}
